package com.zjdiepu.www.manage.api.zpp.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.aliyun.oas.utils.StringUtil;
import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.ZhppDateUtil;
import com.zjdiepu.www.dao.base.DpBaseDealerInfoMapper;
import com.zjdiepu.www.dao.system.BaseBusiUnitMapper;
import com.zjdiepu.www.dao.system.BaseSalersmanMapper;
import com.zjdiepu.www.dao.zpp.PublicDao;
import com.zjdiepu.www.dao.zpp.QueryEntryDao;
import com.zjdiepu.www.dao.zx.ZxQueryCreditApplyMapper;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.model.base.dto.DpBaseDealerInfo;
import com.zjdiepu.www.model.system.dto.BaseBusiUnit;
import com.zjdiepu.www.model.system.dto.BaseSalersman;
import com.zjdiepu.www.model.zpp.pub.ext.DiepuQueryEntryExt;
import com.zjdiepu.www.model.zpp.pub.vo.BusinessListVo;
import com.zjdiepu.www.model.zpp.pub.vo.CreditApplyAddList;
import com.zjdiepu.www.model.zpp.pub.vo.FkshapprovalReturn;
import com.zjdiepu.www.model.zx.dto.ZxQueryCreditApply;

@RestController
@RequestMapping(value= "QueryEntryController", produces="application/json;charset=UTF-8")
public class QueryEntryController extends BaseController
{
	@Resource
	private PublicDao publicDao;
	
	@Resource
	private QueryEntryDao queryEntryDao;
	
	@Resource
	private ICachedDictionaryService iCachedDictionaryService; 
	
	@Resource
	private ZxQueryCreditApplyMapper zxQueryCreditApplyMapper;
	
	@Resource
	private BaseSalersmanMapper baseSalersmanMapper;
	
	@Resource
	private BaseBusiUnitMapper baseBusiUnitMapper;
	
	@Resource
	private DpBaseDealerInfoMapper dpBaseDealerInfoMapper;
	

	//修改所有的查询入口
	@RequestMapping(value = "diepuQueryEntry", method = RequestMethod.POST)
	public Map<String, Object> diepuQueryEntry(DiepuQueryEntryExt diepuQueryEntryExt)throws Exception
	{
		Map<String, Object> maps = success();
		AbstractSysUser currentUser = getCurrentUser();
		
		String userId = currentUser.getId();
		String loginName = currentUser.getName();
		
		if(StringUtil.isBlank(userId) || StringUtil.isBlank(loginName))
		{
			throw new BaseException(ResultFlagType.LOGIN_TIMEOUT.getCode().toString(),ResultFlagType.LOGIN_TIMEOUT.getValue());
		}
		
		if(null == diepuQueryEntryExt)
		{
			throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
		}
		
		String modeIdName = diepuQueryEntryExt.getModeIdName();
		if(StringUtil.isBlank(modeIdName))
		{
			throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
		}
		
		//退回审核列表显示
		if(modeIdName.equals("fkshapprovalReturn"))
		{
			maps = fkshapprovalReturn(diepuQueryEntryExt);
		}
		
		//增加配偶或担保人
		if(modeIdName.equals("creditApplyAddList"))
		{
			maps = creditApplyAddList(diepuQueryEntryExt);
		}
		
		//业务受理>征信查询>征信信息修改
		if(modeIdName.equals("zxQueryCreditChangeList"))
		{
			maps = zxQueryCreditChangeList(diepuQueryEntryExt);
		}
		
		//业务受理>征信查询>征信查询&反馈
		if(modeIdName.equals("zxQueryCreditSearch"))
		{
			maps = zxQueryCreditSearch(diepuQueryEntryExt);
		}
		
		//业务受理>接口征信查询>征信信息修改  杭州工行
		if(modeIdName.equals("autozxQueryCreditChangeList"))
		{
			diepuQueryEntryExt.setZxTypeData("3");
			maps = zxQueryCreditChangeList(diepuQueryEntryExt);
		}
		
		// 业务受理>接口征信查询>增加配偶或担保人
		if(modeIdName.equals("autoCreditApplyAddList"))
		{
			diepuQueryEntryExt.setZxTypeData("3");
			maps = creditApplyAddList(diepuQueryEntryExt);
		}
		//资金流程>退押金流程>客户申请退押金
		if(modeIdName.equals("refundDepositList"))
		{
			maps = refundDepositList(diepuQueryEntryExt);
		}
		return maps;
	}
	
	//资金流程>退押金流程>客户申请退押金
	private Map<String, Object> refundDepositList(DiepuQueryEntryExt diepuQueryEntryExt)throws Exception
	{
		Map<String, Object> maps = success();
		String sqlXmls = "";
		
		//条件查询
		if(StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeStart()) && StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeEnd()))
		{
			int submitTimeStart = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeStart(), ZhppDateUtil.SHORT_DATE_FORMAT);
			int submitTimeEnd = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeEnd(), ZhppDateUtil.SHORT_DATE_FORMAT);
			sqlXmls += " and t.created >= "+submitTimeStart;
			sqlXmls += " and t.created <= "+submitTimeEnd;
		}
		else if(StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeStart()))
		{
			int submitTimeStart = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeStart(), ZhppDateUtil.SHORT_DATE_FORMAT);
			sqlXmls += " and t.created >= "+submitTimeStart;
		}
		else if(StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeEnd()))
		{
			int submitTimeEnd = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeEnd(), ZhppDateUtil.SHORT_DATE_FORMAT);
			sqlXmls += " and t.created <= "+submitTimeEnd;
		}
		else
		{
			if(StringUtil.isBlank(diepuQueryEntryExt.getCustomerName()) && StringUtil.isBlank(diepuQueryEntryExt.getBankName()) && StringUtil.isBlank(diepuQueryEntryExt.getSalesmanName()))
			{
				int submitTimeStart = ZhppDateUtil.dateConvertTimeStamp(ZhppDateUtil.DateToString(new Date(), "yyyy-MM"+"-01"), ZhppDateUtil.SHORT_DATE_FORMAT);
				sqlXmls += " and t.created >= "+submitTimeStart;
			}
		}
		
		if(StringUtil.isNotBlank(diepuQueryEntryExt.getCustomerName()))
		{
			sqlXmls += " and t.customer_name like concat('%','"+diepuQueryEntryExt.getCustomerName()+"', '%') ";
		}
		
		if(StringUtil.isNotBlank(diepuQueryEntryExt.getBankName()))
		{
			sqlXmls += " and t.bank_loan = '"+diepuQueryEntryExt.getBankName()+"' ";
		}

		if(StringUtil.isNotBlank(diepuQueryEntryExt.getSalesmanName()))
		{
			sqlXmls += " and saler.name like concat('%','"+diepuQueryEntryExt.getSalesmanName()+"', '%') ";
		}
		
		AbstractSysUser currentUser = getCurrentUser();
		String userId = currentUser.getId();
		String queryWhereSql = " select t.code_unit from base_account_busi_unit t where 1 = 1 and t.dp_status = 1 and t.user_id = '"+userId+"' ";
		List<Map<String, String>> mapList = publicDao.queryWhereSql(queryWhereSql);
		if(null == mapList || mapList.size() < 1)
		{
			return maps;
		}
		
		sqlXmls += " and ( ";
		Map<String, String> m = mapList.get(0);
		String code_unit = m.get("code_unit").toString();
		sqlXmls += " saler.salersman_code like CONCAT('"+code_unit+"','%') ";
		for(int i=1;i<mapList.size();i++)
		{
			m = mapList.get(i);
			code_unit = m.get("code_unit").toString();
			sqlXmls += " or saler.salersman_code like CONCAT('"+code_unit+"','%') ";
		}
		sqlXmls += " ) ";
		
		sqlXmls += " and t.type_data < 3 ";
		
		sqlXmls += " and t.busi_status = 9999 ";
		
		String countXmls = "SELECT count(t.id)count FROM yw_business t LEFT JOIN base_salersman saler ON saler.id = t.salesman_id WHERE 1 = 1 AND t.dp_status = 1 "+sqlXmls;
		int count = publicDao.countSqlXmls(countXmls);
		if(count > 0)
		{
			diepuQueryEntryExt.setWhereSql(sqlXmls);
			diepuQueryEntryExt.notDeleted();
			PageVo page = getPage(diepuQueryEntryExt);
			List<BusinessListVo> list = queryEntryDao.diepuBusinessList(diepuQueryEntryExt);
			if(null != list && list.size() > 0)
			{
				for(int i=0;i<list.size();i++)
				{
					BusinessListVo businessListVo = list.get(i);
					businessListVo.setChannelType(iCachedDictionaryService.getName(businessListVo.getChannelType(), DataDicType.CHANNEL_TYPE));
					businessListVo.setBankLoan(iCachedDictionaryService.getName(businessListVo.getBankLoan(), DataDicType.LOAN_BANK));
					BaseBusiUnit baseBusiUnit = baseBusiUnitMapper.selectByPrimaryKey(businessListVo.getUnitId());
					if(null != baseBusiUnit)
					{
						businessListVo.setOrgName(baseBusiUnit.getOrgName());
					}
					
					DpBaseDealerInfo dpBaseDealerInfo = dpBaseDealerInfoMapper.selectByPrimaryKey(businessListVo.getDealerId());
					if(null != dpBaseDealerInfo)
					{
						businessListVo.setDealerName(dpBaseDealerInfo.getDealerName());
					}
				}
				page.setRecords(count);
				page.setRows(list);
				maps.put("result", page);
			}
		}
		return maps;
	}
	
	//业务受理>征信查询>征信信息修改
	private Map<String, Object> zxQueryCreditChangeList(DiepuQueryEntryExt diepuQueryEntryExt)throws Exception
	{
		Map<String, Object> maps = success();
		
		String fieldXmls = "";
		String fieldSqlXmls = "";
		String sqlXmls = "";
		
		//条件查询
		if(StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeStart()) && StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeEnd()))
		{
			int submitTimeStart = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeStart(), ZhppDateUtil.SHORT_DATE_FORMAT);
			int submitTimeEnd = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeEnd(), ZhppDateUtil.SHORT_DATE_FORMAT);
			sqlXmls += " and t.created >= "+submitTimeStart;
			sqlXmls += " and t.created <= "+submitTimeEnd;
		}
		else if(StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeStart()))
		{
			int submitTimeStart = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeStart(), ZhppDateUtil.SHORT_DATE_FORMAT);
			sqlXmls += " and t.created >= "+submitTimeStart;
		}
		else if(StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeEnd()))
		{
			int submitTimeEnd = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeEnd(), ZhppDateUtil.SHORT_DATE_FORMAT);
			sqlXmls += " and t.created <= "+submitTimeEnd;
		}
		else
		{
			if(StringUtil.isBlank(diepuQueryEntryExt.getCustomerName()) && StringUtil.isBlank(diepuQueryEntryExt.getBankName()) && StringUtil.isBlank(diepuQueryEntryExt.getSalesmanName()))
			{
				int submitTimeStart = ZhppDateUtil.dateConvertTimeStamp(ZhppDateUtil.DateToString(new Date(), "yyyy-MM"+"-01"), ZhppDateUtil.SHORT_DATE_FORMAT);
				sqlXmls += " and t.created >= "+submitTimeStart;
			}
		}
		
		if(StringUtil.isNotBlank(diepuQueryEntryExt.getCustomerName()))
		{
			sqlXmls += " and t.cus_name like concat('%','"+diepuQueryEntryExt.getCustomerName()+"', '%') ";
		}
		
		if(StringUtil.isNotBlank(diepuQueryEntryExt.getBankName()))
		{
			sqlXmls += " and t.bank_code = '"+diepuQueryEntryExt.getBankName()+"' ";
		}

		if(StringUtil.isNotBlank(diepuQueryEntryExt.getSalesmanName()))
		{
			sqlXmls += " and saler.name like concat('%','"+diepuQueryEntryExt.getSalesmanName()+"', '%') ";
		}
		
		AbstractSysUser currentUser = getCurrentUser();
		String userId = currentUser.getId();
		String queryWhereSql = " select t.code_unit from base_account_busi_unit t where 1 = 1 and t.dp_status = 1 and t.user_id = '"+userId+"' ";
		List<Map<String, String>> mapList = publicDao.queryWhereSql(queryWhereSql);
		if(null == mapList || mapList.size() < 1)
		{
			return maps;
		}
		
		fieldXmls += ",zxquery.credit_result_mark,zxquery.remarks ";
		fieldSqlXmls += " left join zx_query_credit_apply v ";
		fieldSqlXmls += " on t.query_credit_apply_id = v.id  ";
		fieldSqlXmls += " left join base_salersman saler on saler.id = v.salersman_id_then ";
		fieldSqlXmls += " left join zx_credit_query zxquery on zxquery.query_credit_apply_sub_id = t.id ";
		
		sqlXmls += " and ( ";
		Map<String, String> m = mapList.get(0);
		String code_unit = m.get("code_unit").toString();
		sqlXmls += " saler.salersman_code like CONCAT('"+code_unit+"','%') ";
		for(int i=1;i<mapList.size();i++)
		{
			m = mapList.get(i);
			code_unit = m.get("code_unit").toString();
			sqlXmls += " or saler.salersman_code like CONCAT('"+code_unit+"','%') ";
		}
		sqlXmls += " ) ";
		
		if(StringUtil.isBlank(diepuQueryEntryExt.getZxTypeData()))
		{
			sqlXmls += " and t.type_data < 3 ";
		}
		
		if(StringUtil.isNotBlank(diepuQueryEntryExt.getZxTypeData()))
		{
			sqlXmls += " and t.type_data = 3 ";
		}
		 
		sqlXmls += " and zxquery.credit_query_status = 20 ";
		
		sqlXmls += " order by  t.created desc ";
		sqlXmls += " limit 15 ";
		diepuQueryEntryExt.setFieldSqlXmls(fieldSqlXmls);
		diepuQueryEntryExt.setSqlXmls(sqlXmls);
		diepuQueryEntryExt.setFieldXmls(fieldXmls);
		PageVo page = getPage(diepuQueryEntryExt);
		List<CreditApplyAddList> list = queryEntryDao.creditApplyAddList(diepuQueryEntryExt);
		if(null != list && list.size() > 0)
		{
			for(int i=0;i<list.size();i++)
			{
				list.get(i).setBankCode(iCachedDictionaryService.getName(list.get(i).getBankCode(), DataDicType.LOAN_BANK));
				ZxQueryCreditApply zxQueryCreditApply = zxQueryCreditApplyMapper.selectByPrimaryKey(list.get(i).getTid());
				if(null != zxQueryCreditApply)
				{
					BaseSalersman baseSalersman = baseSalersmanMapper.selectByPrimaryKey(zxQueryCreditApply.getSalersmanIdThen());
					if(null != baseSalersman)
					{
						list.get(i).setCreatorName(baseSalersman.getName());
						BaseBusiUnit baseBusiUnit = baseBusiUnitMapper.selectByPrimaryKey(baseSalersman.getUnitId());
						if(null != baseBusiUnit)
						{
							list.get(i).setName(baseBusiUnit.getOrgName());
						}
					}
				}
				
				if(StringUtil.isNotBlank(list.get(i).getReasonBack()))
				{
					if(StringUtil.isBlank(diepuQueryEntryExt.getZxTypeData()))
					{
						list.get(i).setReasonBack(iCachedDictionaryService.getName(list.get(i).getReasonBack(), DataDicType.CREDIT_RETURNED_REASON));
					}
					
					if(list.get(i).getReasonBack().equals("099"))
					{
						list.get(i).setReasonBack("工行退回错误");
					}
				}
			}
			
	        Map<String, String> loanBank = iCachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.LOAN_BANK,"请选择...");
            maps.put("loanBank", loanBank);
			
			page.setRecords(list.size());
			page.setRows(list);
		}
		maps.put("result", page);
		return maps;
	}
	
	//业务受理>征信查询>增加配偶或担保人
	private Map<String, Object> creditApplyAddList(DiepuQueryEntryExt diepuQueryEntryExt)throws Exception
	{
		Map<String, Object> maps = success();
		
		String fieldSqlXmls = "";
		String sqlXmls = "";
		
		//条件查询
		if(StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeStart()) && StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeEnd()))
		{
			int submitTimeStart = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeStart(), ZhppDateUtil.SHORT_DATE_FORMAT);
			int submitTimeEnd = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeEnd(), ZhppDateUtil.SHORT_DATE_FORMAT);
			sqlXmls += " and t.created >= "+submitTimeStart;
			sqlXmls += " and t.created <= "+submitTimeEnd;
		}
		else if(StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeStart()))
		{
			int submitTimeStart = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeStart(), ZhppDateUtil.SHORT_DATE_FORMAT);
			sqlXmls += " and t.created >= "+submitTimeStart;
		}
		else if(StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeEnd()))
		{
			int submitTimeEnd = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeEnd(), ZhppDateUtil.SHORT_DATE_FORMAT);
			sqlXmls += " and t.created <= "+submitTimeEnd;
		}
		else
		{
			if(StringUtil.isBlank(diepuQueryEntryExt.getCustomerName()) && StringUtil.isBlank(diepuQueryEntryExt.getBankName()) && StringUtil.isBlank(diepuQueryEntryExt.getSalesmanName()))
			{
				int submitTimeStart = ZhppDateUtil.dateConvertTimeStamp(ZhppDateUtil.DateToString(new Date(), "yyyy-MM"+"-01"), ZhppDateUtil.SHORT_DATE_FORMAT);
				sqlXmls += " and t.created >= "+submitTimeStart;
			}
		}
		
		if(StringUtil.isNotBlank(diepuQueryEntryExt.getCustomerName()))
		{
			sqlXmls += " and t.cus_name like concat('%','"+diepuQueryEntryExt.getCustomerName()+"', '%') ";
		}
		
		if(StringUtil.isNotBlank(diepuQueryEntryExt.getBankName()))
		{
			sqlXmls += " and t.bank_code = '"+diepuQueryEntryExt.getBankName()+"' ";
		}

		if(StringUtil.isNotBlank(diepuQueryEntryExt.getSalesmanName()))
		{
			sqlXmls += " and saler.name like concat('%','"+diepuQueryEntryExt.getSalesmanName()+"', '%') ";
		}
		
		AbstractSysUser currentUser = getCurrentUser();
		String userId = currentUser.getId();
		String queryWhereSql = " select t.code_unit from base_account_busi_unit t where 1 = 1 and t.dp_status = 1 and t.user_id = '"+userId+"' ";
		List<Map<String, String>> mapList = publicDao.queryWhereSql(queryWhereSql);
		if(null == mapList || mapList.size() < 1)
		{
			return maps;
		}
		
		fieldSqlXmls += " left join zx_query_credit_apply v ";
		fieldSqlXmls += " on t.query_credit_apply_id = v.id  ";
		fieldSqlXmls += " left join base_salersman saler on saler.id = v.salersman_id_then ";
		
		sqlXmls += " and ( ";
		Map<String, String> m = mapList.get(0);
		String code_unit = m.get("code_unit").toString();
		sqlXmls += " saler.salersman_code like CONCAT('"+code_unit+"','%') ";
		for(int i=1;i<mapList.size();i++)
		{
			m = mapList.get(i);
			code_unit = m.get("code_unit").toString();
			sqlXmls += " or saler.salersman_code like CONCAT('"+code_unit+"','%') ";
		}
		sqlXmls += " ) ";
		
		if(StringUtil.isBlank(diepuQueryEntryExt.getZxTypeData()))
		{
			sqlXmls += " and t.type_data < 3 ";
		}
		
		if(StringUtil.isNotBlank(diepuQueryEntryExt.getZxTypeData()))
		{
			sqlXmls += " and t.type_data = 3 ";
		}
		
		sqlXmls += " and t.credit_people_type = 03";
		sqlXmls += " order by  t.created desc ";
		sqlXmls += " limit 15 ";
		diepuQueryEntryExt.setFieldSqlXmls(fieldSqlXmls);
		diepuQueryEntryExt.setSqlXmls(sqlXmls);
		PageVo page = getPage(diepuQueryEntryExt);
		List<CreditApplyAddList> list = queryEntryDao.creditApplyAddList(diepuQueryEntryExt);
		if(null != list && list.size() > 0)
		{
			for(int i=0;i<list.size();i++)
			{
				list.get(i).setBankCode(iCachedDictionaryService.getName(list.get(i).getBankCode(), DataDicType.LOAN_BANK));
				ZxQueryCreditApply zxQueryCreditApply = zxQueryCreditApplyMapper.selectByPrimaryKey(list.get(i).getTid());
				if(null != zxQueryCreditApply)
				{
					BaseSalersman baseSalersman = baseSalersmanMapper.selectByPrimaryKey(zxQueryCreditApply.getSalersmanIdThen());
					if(null != baseSalersman)
					{
						list.get(i).setCreatorName(baseSalersman.getName());
						BaseBusiUnit baseBusiUnit = baseBusiUnitMapper.selectByPrimaryKey(baseSalersman.getUnitId());
						if(null != baseBusiUnit)
						{
							list.get(i).setName(baseBusiUnit.getOrgName());
						}
					}
				}
			}
			
	        Map<String, String> loanBank = iCachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.LOAN_BANK,"请选择...");
            maps.put("loanBank", loanBank);
			
			page.setRecords(list.size());
			page.setRows(list);
		}
		maps.put("result", page);
		return maps;
	}
	
	//业务受理>征信查询>征信查询&反馈
	private Map<String, Object> zxQueryCreditSearch(DiepuQueryEntryExt diepuQueryEntryExt)throws Exception
	{
		Map<String, Object> maps = success();
		
		String fieldXmls = "";
		String fieldSqlXmls = "";
		String sqlXmls = "";
		
		//条件查询
		if(StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeStart()) && StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeEnd()))
		{
			int submitTimeStart = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeStart(), ZhppDateUtil.SHORT_DATE_FORMAT);
			int submitTimeEnd = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeEnd(), ZhppDateUtil.SHORT_DATE_FORMAT);
			sqlXmls += " and t.created >= "+submitTimeStart;
			sqlXmls += " and t.created <= "+submitTimeEnd;
		}
		else if(StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeStart()))
		{
			int submitTimeStart = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeStart(), ZhppDateUtil.SHORT_DATE_FORMAT);
			sqlXmls += " and t.created >= "+submitTimeStart;
		}
		else if(StringUtils.isNoneBlank(diepuQueryEntryExt.getSubmitTimeEnd()))
		{
			int submitTimeEnd = ZhppDateUtil.dateConvertTimeStamp(diepuQueryEntryExt.getSubmitTimeEnd(), ZhppDateUtil.SHORT_DATE_FORMAT);
			sqlXmls += " and t.created <= "+submitTimeEnd;
		}
		else
		{
			if(StringUtil.isBlank(diepuQueryEntryExt.getCustomerName()) && StringUtil.isBlank(diepuQueryEntryExt.getBankName()) && StringUtil.isBlank(diepuQueryEntryExt.getSalesmanName()))
			{
				int submitTimeStart = ZhppDateUtil.dateConvertTimeStamp(ZhppDateUtil.DateToString(new Date(), "yyyy-MM"+"-01"), ZhppDateUtil.SHORT_DATE_FORMAT);
				sqlXmls += " and t.created >= "+submitTimeStart;
			}
		}
		
		if(StringUtil.isNotBlank(diepuQueryEntryExt.getCustomerName()))
		{
			sqlXmls += " and t.cus_name like concat('%','"+diepuQueryEntryExt.getCustomerName()+"', '%') ";
		}
		
		if(StringUtil.isNotBlank(diepuQueryEntryExt.getBankName()))
		{
			sqlXmls += " and t.bank_code = '"+diepuQueryEntryExt.getBankName()+"' ";
		}

		if(StringUtil.isNotBlank(diepuQueryEntryExt.getSalesmanName()))
		{
			sqlXmls += " and saler.name like concat('%','"+diepuQueryEntryExt.getSalesmanName()+"', '%') ";
		}
		
		AbstractSysUser currentUser = getCurrentUser();
		String userId = currentUser.getId();
		String queryWhereSql = " select t.code_unit from base_account_busi_unit t where 1 = 1 and t.dp_status = 1 and t.user_id = '"+userId+"' ";
		List<Map<String, String>> mapList = publicDao.queryWhereSql(queryWhereSql);
		if(null == mapList || mapList.size() < 1)
		{
			return maps;
		}
		fieldXmls += " ,zxquery.id hid,v.id tid ";
		fieldSqlXmls += " left join zx_query_credit_apply v ";
		fieldSqlXmls += " on t.query_credit_apply_id = v.id  ";
		fieldSqlXmls += " left join base_salersman saler on saler.id = v.salersman_id_then ";
		fieldSqlXmls += " left join zx_credit_query zxquery on zxquery.query_credit_apply_sub_id = t.id ";
		
		sqlXmls += " and ( ";
		Map<String, String> m = mapList.get(0);
		String code_unit = m.get("code_unit").toString();
		sqlXmls += " saler.salersman_code like CONCAT('"+code_unit+"','%') ";
		for(int i=1;i<mapList.size();i++)
		{
			m = mapList.get(i);
			code_unit = m.get("code_unit").toString();
			sqlXmls += " or saler.salersman_code like CONCAT('"+code_unit+"','%') ";
		}
		sqlXmls += " ) ";
		
		if(StringUtil.isBlank(diepuQueryEntryExt.getZxTypeData()))
		{
			sqlXmls += " and t.type_data < 3 ";
		}
		
		if(StringUtil.isNotBlank(diepuQueryEntryExt.getZxTypeData()))
		{
			sqlXmls += " and t.type_data = 3 ";
		}
		
		sqlXmls += " and zxquery.credit_query_status = 10 ";
		sqlXmls += " and v.query_credit_apply_status = 20 ";
		
		sqlXmls += " order by  t.created desc ";
		sqlXmls += " limit 15 ";
		diepuQueryEntryExt.setFieldXmls(fieldXmls);
		diepuQueryEntryExt.setFieldSqlXmls(fieldSqlXmls);
		diepuQueryEntryExt.setSqlXmls(sqlXmls);
		PageVo page = getPage(diepuQueryEntryExt);
		List<CreditApplyAddList> list = queryEntryDao.creditApplyAddList(diepuQueryEntryExt);
		if(null != list && list.size() > 0)
		{
			for(int i=0;i<list.size();i++)
			{
				list.get(i).setBankCode(iCachedDictionaryService.getName(list.get(i).getBankCode(), DataDicType.LOAN_BANK));
				list.get(i).setReasonBack(iCachedDictionaryService.getName(list.get(i).getReasonBack(), DataDicType.CREDIT_RETURNED_REASON));
				ZxQueryCreditApply zxQueryCreditApply = zxQueryCreditApplyMapper.selectByPrimaryKey(list.get(i).getTid());
				if(null != zxQueryCreditApply)
				{
					BaseSalersman baseSalersman = baseSalersmanMapper.selectByPrimaryKey(zxQueryCreditApply.getSalersmanIdThen());
					if(null != baseSalersman)
					{
						list.get(i).setCreatorName(baseSalersman.getName());
						BaseBusiUnit baseBusiUnit = baseBusiUnitMapper.selectByPrimaryKey(baseSalersman.getUnitId());
						if(null != baseBusiUnit)
						{
							list.get(i).setName(baseBusiUnit.getOrgName());
						}
					}
				}
				
				String cusNo = list.get(i).getCusNo();
				sqlXmls = "select t.id,date_format(t.start_date,'%Y-%m-%d %H:%i:%s')start_date from zx_credit_query t left join zx_query_credit_apply_sub p on p.id = t.query_credit_apply_sub_id where 1 = 1 and t.end_date is not null and t.credit_query_status = 40 and t.dp_status = 1 and p.cus_no = '"+cusNo+"' order by t.created desc limit 1";
            	List<Map<String, String>> resutMpas = publicDao.queryWhereSql(sqlXmls);
            	//如果当前的这条数据信息时间小于  征信保存的结束时间  则标颜色
            	if(null != resutMpas && 0 != resutMpas.size())
            	{
            		for(int j=0;j<resutMpas.size();j++)
            		{
            			//当前查询列表时间
                		long crdateTime = list.get(i).getCrdateTime();
                		
                		//当前的Map
                		Map<String, String> map = resutMpas.get(j);
                		
                		//符合身份证表的时间
                		String start_date = map.get("start_date").toString();
                		long time = ZhppDateUtil.dateConvertTimeStamp(start_date, ZhppDateUtil.LONG_DATE_FORMAT) + 86399;
                		
                		long d_time = crdateTime - time;
            			
            			if(d_time < (86400 * 30))
            			{
            				list.get(i).setTimeFlag(1);
            				break;
            			}
            		}
            	}
			}
			
	        Map<String, String> loanBank = iCachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.LOAN_BANK,"请选择...");
            maps.put("loanBank", loanBank);
			
			page.setRecords(list.size());
			page.setRows(list);
		}
		maps.put("result", page);
		return maps;
	}
	
	//退回审核列表显示
	private Map<String, Object> fkshapprovalReturn(DiepuQueryEntryExt diepuQueryEntryExt)
	{
		Map<String, Object> maps = success();
		String busiId = diepuQueryEntryExt.getBusiId();
		String modeId = diepuQueryEntryExt.getModeId();
		if(StringUtil.isBlank(busiId) || StringUtil.isBlank(modeId))
		{
			throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
		}
		String[] modeIdArry = {ModeIDType.CSDZS.getCode(),ModeIDType.ZSYS.getCode(),ModeIDType.ZSES.getCode(),ModeIDType.DSCS.getCode(),ModeIDType.DSFS.getCode(),ModeIDType.FKJLSH.getCode(),ModeIDType.FKZJSH.getCode()};  
		boolean modeBoolean = false;
		if(null != modeIdArry && modeIdArry.length > 0)
		{
			for(int i=0;i<modeIdArry.length;i++)
			{
				if(modeId.equals(modeIdArry[i]))
				{
					modeBoolean = true;
					break;
				}
			}
		}
		if(!modeBoolean)
		{
			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"没有找到退回相对应的节点");
		}
		String sqlXmls = "";
		if(modeId.equals(ModeIDType.CSDZS.getCode()))
		{
			sqlXmls += " and t.mode_id = '"+ModeIDType.CSDCS.getCode()+"' ";
		}
		else if(modeId.equals(ModeIDType.ZSYS.getCode()))
		{
			sqlXmls += " and t.mode_id in ('"+ModeIDType.CSDCS.getCode()+"','"+ModeIDType.CSDZS.getCode()+"') ";
		}
		else if(modeId.equals(ModeIDType.ZSES.getCode()))
		{
			sqlXmls += " and t.mode_id in ('"+ModeIDType.CSDCS.getCode()+"','"+ModeIDType.CSDZS.getCode()+"','"+ModeIDType.ZSYS.getCode()+"') ";
		}
		else if(modeId.equals(ModeIDType.DSCS.getCode()))
		{
			sqlXmls += " and t.mode_id in ('"+ModeIDType.CSDCS.getCode()+"','"+ModeIDType.CSDZS.getCode()+"') ";
		}
		else if(modeId.equals(ModeIDType.DSFS.getCode()))
		{
			sqlXmls += " and t.mode_id in ('"+ModeIDType.CSDCS.getCode()+"','"+ModeIDType.CSDZS.getCode()+"','"+ModeIDType.DSCS.getCode()+"') ";
		}
		else if(modeId.equals(ModeIDType.FKZJSH.getCode()))
		{
			sqlXmls += " and t.mode_id in ('"+ModeIDType.CSDCS.getCode()+"','"+ModeIDType.CSDZS.getCode()+"','"+ModeIDType.ZSYS.getCode()+"','"+ModeIDType.ZSES.getCode()+"','"+ModeIDType.DSCS.getCode()+"','"+ModeIDType.DSFS.getCode()+"') ";
		}
		else if(modeId.equals(ModeIDType.FKZJSH.getCode()))
		{
			sqlXmls += " and t.mode_id in ('"+ModeIDType.CSDCS.getCode()+"','"+ModeIDType.CSDZS.getCode()+"','"+ModeIDType.ZSYS.getCode()+"','"+ModeIDType.ZSES.getCode()+"','"+ModeIDType.DSCS.getCode()+"','"+ModeIDType.DSFS.getCode()+"','"+ModeIDType.FKJLSH.getCode()+"') ";
		}
		sqlXmls += " and t.busi_id = '"+busiId+"' ";
		sqlXmls += " group by t.mode_id ";
		sqlXmls += " order by t.created asc ";
		List<FkshapprovalReturn> result = queryEntryDao.fkshapprovalReturn(sqlXmls);
		PageVo page = getPage(diepuQueryEntryExt);
		if(null != result && result.size() > 0)
		{
			String[] modeIdOrderArry = {ModeIDType.CSDCS.getCode(),ModeIDType.CSDZS.getCode(),ModeIDType.ZSYS.getCode(),ModeIDType.ZSES.getCode(),ModeIDType.DSCS.getCode(),ModeIDType.DSFS.getCode(),ModeIDType.FKJLSH.getCode()};  
			List<FkshapprovalReturn> list = new ArrayList<FkshapprovalReturn>();
			if(null != modeIdOrderArry && modeIdOrderArry.length > 0)
			{
				for(int j=0;j<result.size();j++)
				{
					if(result.get(j).getModeId().equals(ModeIDType.CSDCS.getCode()))
					{
						list.add(result.get(j));
						break;
					}
				}
				for(int j=0;j<result.size();j++)
				{
					if(result.get(j).getModeId().equals(ModeIDType.CSDZS.getCode()))
					{
						list.add(result.get(j));
						break;
					}
				}
				for(int j=0;j<result.size();j++)
				{
					if(result.get(j).getModeId().equals(ModeIDType.ZSYS.getCode()))
					{
						list.add(result.get(j));
						break;
					}
				}
				for(int j=0;j<result.size();j++)
				{
					if(result.get(j).getModeId().equals(ModeIDType.ZSES.getCode()))
					{
						list.add(result.get(j));
						break;
					}
				}
				for(int j=0;j<result.size();j++)
				{
					if(result.get(j).getModeId().equals(ModeIDType.DSCS.getCode()))
					{
						list.add(result.get(j));
						break;
					}
				}
				for(int j=0;j<result.size();j++)
				{
					if(result.get(j).getModeId().equals(ModeIDType.DSFS.getCode()))
					{
						list.add(result.get(j));
						break;
					}
				}
				for(int j=0;j<result.size();j++)
				{
					if(result.get(j).getModeId().equals(ModeIDType.FKJLSH.getCode()))
					{
						list.add(result.get(j));
						break;
					}
				}
			}
			page.setRecords(list.size());
			page.setRows(list);
		}
        maps.put("result", page);
		return maps;
	}
}
